package cn.m3a1.task;

import cn.m3a1.dao.UserDao;
import cn.m3a1.model.Syslog;
import cn.m3a1.model.User;
import com.iceking.dynamicweb.*;
import net.sf.json.JSONObject;

import java.util.List;

/**
 * Created by wyj on 2019-05-31.
 */
public class AdminTask extends TaskProcessor {
   private User loginUser;
    public Boolean panduan(){
        loginUser = (User) getSession().getAttribute("loginUser");
        if (loginUser==null){
            return false;
        }

        int type=loginUser.getType();

        if (type!=99){
            return false;
        }
        return  true;
    }
    /**
     * 新增用户
     * @throws Exception
     */
    public void save() throws Exception {

      if (!panduan()){
          return;
      }
        JSONObject json = new JSONObject();
        User user = new User();
        user.setCode(getParam("code"));
        user.setName(getParam("name"));
        user.setEmail(getParam("email"));
        user.setUsername(getParam("username"));
        user.setTel(getParam("tel"));
        user.setPassword(GlobalUtils.md5Encode(getParam("password")));
        user.setType(getParamInt("type"));
        getDBConn().stopTransaction();
        //根据用户名和密码去进行数据库访问
        Pagination<User> pg = new Pagination<>();
        pg.appendWhere("where username=?", user.getUsername());
        getDBConn().select(User.class, pg);
        User user1 = pg.getFirstResult();
        if (user1 == null) {
            DBConnection connection = DWPConsole.getInstance().getOneConnection();
            connection.save(user);
            connection.close();

            getDBConn().stopTransaction();
            json.put("succ", true);
            json.put("stmt", "添加成功");
            toJson(json.toString());
        }else {
            json.put("succ", false);
            json.put("stmt", "添加失败");
            toJson(json.toString());
        }
    }

    public void finduser() throws Exception {
        if (!panduan()){
            return;
        }
        User loginUser = (User) getSession().getAttribute("loginUser");
        if (loginUser==null){
            return;
        }

        int type=loginUser.getType();

        if (type!=99){
            return;
        }


        JSONObject json = new JSONObject();

        int id = getParamInt("id");
        System.out.println("id=" + id);
        //根据用户名和密码去进行数据库访问
        Pagination<User> pg = new Pagination<>();
        pg.appendWhere("where id=?", id);
        getDBConn().select(User.class, pg);
        User user = pg.getFirstResult();
        if (user != null) {

            json.put("succ", true);
            json.put("user", user);
            toJson(json.toString());
        }
    }

    /**
     * 管理员修改页面
     *
     * @throws Exception
     */
    public void update() throws Exception {
        if (!panduan()){
            return;
        }
        JSONObject json = new JSONObject();
        User user = new User();
        user.setId(getParamInt("userid"));
        Pagination<User> pg = new Pagination<>();
        pg.appendWhere("where id=?", user.getId());
        getDBConn().select(User.class, pg);

        user = pg.getFirstResult();
        user.toString();
        user.setEmail(getParam("upemail"));
        user.setPassword(GlobalUtils.md5Encode(getParam("uppassword")));
        user.setTel(getParam("uptel"));
        getDBConn().save(user);
        json.put("succ", true);
        json.put("stmt", "更改成功");
        toJson(json.toString());
    }

    /**
     * 根据id删除数据
     * @throws Exception
     */
    public void del() throws Exception {
        if (!panduan()){
            return;
        }
        int id = getParamInt("id");
        System.out.println("del"+id);
        if (id>1){
            User user = getDBConn().findByPK(User.class, id);
            getDBConn().delete(user);
        }
    }
    /**
     * 转发跳转到管理员管理页面
     * @throws Exception
     */
    public void home() throws  Exception{
        if (!panduan()){
            return;
        }
        String username = getParam("username");
        int usertype = getParamInt("type");
        System.out.println(username+usertype);
        UserDao userDao=new UserDao();
        List list;
        FrmkView frmkView;

        int type=loginUser.getType();
        //验证身份
        if (type==99){
            //判断是否是超级管理员
            frmkView=new FrmkView("Administration.html");
            //根据用户名和密码去进行数据库访问
            Pagination<User> pg = new Pagination<>();
            if (usertype==0){
                if (GlobalUtils.isEmpty(username)){

                    list = userDao.findallUser(getDBConn());
                }else {
                    pg.appendWhere("where username=?",username);
                    getDBConn().select(User.class, pg);
                   list = pg.getRows();
                }

            }else{
                if (GlobalUtils.isEmpty(username)){
                    pg.appendWhere("where type=?",usertype);
                    getDBConn().select(User.class, pg);
                    list = pg.getRows();
                }else {
                    pg.appendWhere("where username=? and type=?",username,usertype);
                    getDBConn().select(User.class, pg);
                    list = pg.getRows();
                }


            }


            frmkView.addData("members",list);
            //判断是否是班主任
        }else{
            //非法用户
            frmkView=new FrmkView("error.html");
        }

        frmkView.addData("loginUser",loginUser);
        toFreemarker(frmkView);

    }
}
